package cn.lanqiao.active;

public class VolatileDemo {
    public static void main(String[] args) {
        Object lock = new Object();
        MyThread myThread = new MyThread();
        myThread.start();
        while (true) {

                if (myThread.isFlag()) {
                    System.out.println("修改完毕");
                    break;
                }

        }
    }

    static class MyThread extends Thread {
        private volatile boolean flag = false;

        @Override
        public void run() {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            flag = true;
        }

        public boolean isFlag() {
            return flag;
        }

        public void setFlag(boolean flag) {
            this.flag = flag;
        }
    }
}
